<template>
  <div>
    <nav-bar class="fix">
      <template v-slot:left
        ><img src="~assets/image/detail/back.png" alt="" @click="backClick"
      /></template>
      <template v-slot:center>
        <div class="detail">
          <div
            v-for="(item, index) in title"
            :key="index"
            :class="{ active: index === currentIndex }"
            @click="itemClick(index)"
          >
            {{ item }}
          </div>
        </div>
      </template>
    </nav-bar>
    <bscroll @scroll="contentScroll" ref="scroll1" :probe-type="3">
      <detail-swiper :topImages="topImages" class="swiperTop"></detail-swiper>
      <detail-base-info :goods="goods" />
      <detail-shop-info :shop="shop" />
      <detail-goods-info :detail-info="detailInfo" />
      <detail-param-info ref="param" :param-info="paramInfo" />
      <detail-comment-info ref="commen" :comment-info="commentInfo" />
      <good-list ref="recommend" class="goods" />
    </bscroll>
    <detail-bottom-bar @addCart="addToCart" />
    <back-top @click.native="backtopClick" v-show="isShow"></back-top>
  </div>
</template>
<script>
import {
  getDetail,
  Goods,
  Shop,
  GoodsParams,
  getRecommend,
} from "network/detail";

// import Bscroll from "components/commen/bscroll/Bscroll";
import { backTop, bscroll } from "commen/mixin";
import { debouse } from "commen/date";

import NavBar from "components/commen/navbar/NavBar";
import DetailSwiper from "./detailchild/DetailSwiper";
import DetailBaseInfo from "./detailchild/DetailBaseInfo";
import DetailShopInfo from "./detailchild/DetailShopInfo";
import DetailCommentInfo from "./detailchild/DetailCommentInfo";
import DetailGoodsInfo from "./detailchild/DetailGoodsInfo";
import DetailParamInfo from "./detailchild/DetailParamInfo";
import DetailBottomBar from "./detailchild/DetailBottomBar";
import GoodList from "components/content/goods/GoodList";
export default {
  name: "Detail",
  components: {
    // Bscroll,
    NavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailCommentInfo,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailBottomBar,
    GoodList,
  },
  mixins: [bscroll, backTop],
  data() {
    return {
      iid: null,
      title: ["商品", "参数", "评论", "推荐"],
      currentIndex: 0,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      themeTopYs: [],
      getThemeTopy: null,
    };
  },
  methods: {
    itemClick(index) {
      this.currentIndex = index;
      this.$refs.scroll1.scrollTo(0, -this.themeTopYs[index], 200);
    },
    backClick() {
      this.$router.back();
    },
    backtopClick() {
      this.$refs.scroll1.scrollTo(0, 0);
    },
    addToCart() {
      //1 获取购物车需要展示的数据
      const product = {};
      product.image = this.topImages[0];
      product.title = this.goods.title;
      product.desc = this.goods.desc;
      product.price = this.goods.realPrice;
      product.iid = this.iid;
      //2 将商品将入购物车
      this.$store.dispatch("addCart", product).then((res) => {
        // console.log("res");
        // console.log(this.$toast);
        this.$toast;
      });
      //3 添加到购物车成功
    },
    contentScroll(position) {
      // console.log(-position.y);
      this.getThemeTopy();
      const positionY = -position.y;
      for (var i = 0; i <= this.themeTopYs.length; i++) {
        if (
          positionY >= this.themeTopYs[i] &&
          positionY < this.themeTopYs[i + 1]
        ) {
          this.currentIndex = i;
        }
      }
      // switch (positionY) {
      //   case positionY > this.themeTopYs[3]:
      //     this.currentIndex = 3;
      //     console.log(themeTopYs[3]);
      // }
    },
  },

  created() {
    this.iid = this.$route.params.iid;
    getDetail(this.iid).then((res) => {
      const data = res.result;
      // console.log(res);
      //轮播图
      this.topImages = data.itemInfo.topImages;

      //3.获取商品信息
      this.goods = new Goods(
        data.itemInfo,
        data.columns,
        data.shopInfo.services
      );
      //4.创建店铺信息对象
      this.shop = new Shop(data.shopInfo);
      //5.保存商品的详情数据
      this.detailInfo = data.detailInfo;
      //6.获取参数信息
      this.paramInfo = new GoodsParams(
        data.itemParams.info,
        data.itemParams.rule
      );
      //7.获取评论信息
      if (data.rate.cRate !== 0) {
        this.commentInfo = data.rate.list[0];
      }
    });
    //3.获取getRecommend推荐信息
    getRecommend().then((res) => {
      this.recommend = res.data.list;
    });
    this.getThemeTopy = debouse(() => {
      this.themeTopYs = [];
      this.themeTopYs.push(0);
      this.themeTopYs.push(this.$refs.param.$el.offsetTop);
      this.themeTopYs.push(this.$refs.commen.$el.offsetTop);
      this.themeTopYs.push(this.$refs.recommend.$el.offsetTop);
      // console.log(this.themeTopYs);
      return this.themeTopYs;
    });
  },
  beforeUpdate() {},
};
</script>
<style scoped>
.detail {
  display: flex;
  font-size: 14px;
}
.detail div {
  flex: 1;
}
img {
  width: 16px;
}
.active {
  color: red;
}
.fix {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 99;
  background-color: #fff;
}

.bottom {
  z-index: 9999;
}
.goods {
  margin-top: 20px;
  padding-top: 10px;
  border-top: 3px solid #f5f4f4;
}
</style>